package com.lou.springboot.controller.admin;

import com.lou.springboot.common.ResultVO;
import com.lou.springboot.common.ResultVOUtil;
import com.lou.springboot.entity.Reader;
import com.lou.springboot.enums.ResultEnum;
import com.lou.springboot.service.ReaderService;
import com.lou.springboot.utils.IpUtils;
import com.lou.springboot.utils.JsonUtils;
import com.lou.springboot.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class ReaderController {

    @Autowired
    private ReaderService readerService;

    @GetMapping("/readers")
    public String list(HttpServletRequest request) {
        request.setAttribute("path", "readers");
        return "admin/reader";
    }

    @GetMapping("/readers/list")
    @ResponseBody
    public ResultVO list(@RequestParam Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("page")) || StringUtils.isEmpty(map.get("limit"))) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        PageUtil pageUtil = new PageUtil(map);
        return ResultVOUtil.success(readerService.getReaderList(pageUtil));
    }

//    @GetMapping("/readers/save")
//    @ResponseBody
//    public ResultVO saveReader(HttpServletRequest request) {
//        String addressIp = IpUtils.getIpAddr(request);
//        // 根据ip获取用户信息，如果用户第一次访问就创建用户，否则更新用户访问次数
//        Reader reader = readerService.getReaderByIp(addressIp);
//        if (reader == null) {
//            if (!readerService.addReader(addressIp)) {
//                return ResultVOUtil.error(ResultEnum.READER_ADD_FAILED);
//            }
//        }
//        return ResultVOUtil.success();
//    }
}
